﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotCMIS;
using DotCMIS.Client.Impl;
using DotCMIS.Client;
using DotCMIS.Data.Impl;
using DotCMIS.Data.Extensions;
using System.IO;

public partial class Demo : System.Web.UI.Page
{
   



    protected void Page_Load(object sender, EventArgs e)
    {
         //define dictonary with key value pair
        Dictionary<string, string> parameters = new Dictionary<string, string>();

        // define binding type, in our example we are using ATOMPUB as stated above
        parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
        // define CMIS available path which is already available under alfresco
        parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom";
        // alfresco portal admin user name
        parameters[DotCMIS.SessionParameter.User] = "admin";
        // alfresco portal admin password
        parameters[DotCMIS.SessionParameter.Password] = "Diciembre11";
        // define session factory
        SessionFactory factory = SessionFactory.NewInstance();
        //IList<IRepository> repos = factory.GetRepositories(parameters);

        // using session factory get the default repository, on this repository we would be performing actions & create session on this repository
        ISession session = factory.GetRepositories(parameters)[0].CreateSession();

    }
    protected void btnCrearFolder_Click()
    {
        // define dictonary with key value pair
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        // define binding type, in our example we are using ATOMPUB as stated above
        parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
        // define CMIS available path which is already available under alfresco
        parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom";
        // alfresco portal admin user name
        parameters[DotCMIS.SessionParameter.User] = "admin";
        // alfresco portal admin password
        parameters[DotCMIS.SessionParameter.Password] = "Diciembre11";
        // define session factory
        SessionFactory factory = SessionFactory.NewInstance();
        // using session factory get the default repository, on this repository we would be performing actions & create session on this repository
        ISession session = factory.GetRepositories(parameters)[0].CreateSession();
        // get the root folder in which we would be creating a new folder
        IFolder root = session.GetRootFolder();
        // print statement before creating a folder
        Response.Write("<br /> Creating 'NewFolder: Surya' in the root folder");
        // define dictory with key value pair for new folder that we are going to create
        IDictionary<string, object> properties = new Dictionary<string, object>();
        // below property define name
        properties.Add(PropertyIds.Name, "Surya");
        // below propert define object type & object types can be folder, document etc.., in our case it is folder
        properties.Add(PropertyIds.ObjectTypeId, "cmis:folder");
        // create a folder in root folder which we defined above @ session.GetRootFolder()
        IFolder newFolder = root.CreateFolder(properties);
        // check to see did it work?
        IItemEnumerable<ICmisObject> childrens = root.GetChildren();
        // writing all folders
        Response.Write("Now finding the following objects in the root folder:-");
        foreach (ICmisObject item in childrens)
        {
            Response.Write("<br /> â€“Name: " + item.Name);
        }
    }
    protected void btnNavegarFolder_Click(object sender, EventArgs e)
    {
        ConnectingUsingAtomPub_FolderNavigation();
    }

    private void ConnectingUsingAtomPub_FolderNavigation()
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
        //parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom";
        parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://127.0.0.1:8080/alfresco/service/api/cmis";
        
        parameters[DotCMIS.SessionParameter.User] = "admin";
        parameters[DotCMIS.SessionParameter.Password] = "Diciembre11";
        SessionFactory factory = SessionFactory.NewInstance();
        // create a session object for default repository
        ISession session = factory.GetRepositories(parameters)[0].CreateSession();
        // get all folders
        IFolder root = session.GetRootFolder();
        // print each object
        foreach (ITree<IFileableCmisObject> t in root.GetDescendants(-1))
        {
            PrintTree(t);
        }
    }

    private void PrintTree(ITree<IFileableCmisObject> tree)
    {
        Response.Write("<br />Descendant " + tree.Item.Name + "ID" + tree.Item.Id);
        if (tree.Children != null)
        {
            foreach (ITree<IFileableCmisObject> treeItem in tree.Children)
            {
                PrintTree(treeItem);
            }
        }
    }
    protected void btnCrearFolder_Click1(object sender, EventArgs e)
    {
        btnCrearFolder_Click();
    }
    protected void btnNavegarFolder_Click1(object sender, EventArgs e)
    {
        ConnectingUsingAtomPub_FolderNavigation();
    }

    private void ConnectingUsingAtomPub_CreateDocument()
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
        parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://127.0.0.1:8080/alfresco/service/api/cmis";
        parameters[DotCMIS.SessionParameter.User] = "admin";
        parameters[DotCMIS.SessionParameter.Password] = "Diciembre11";
        SessionFactory factory = SessionFactory.NewInstance();
        ISession session = factory.GetRepositories(parameters)[0].CreateSession();
        IOperationContext oc = session.CreateOperationContext();
        oc.IncludeAcls = true;
        IFolder folder = session.GetRootFolder();
        // document name
        string formattedName = "SuryaPrakashTesting.doc";
        // define dictionary
        IDictionary<string, object> properties = new Dictionary<string, object>();
        properties.Add(PropertyIds.Name, formattedName);
        // define object type as document, as we wanted to create document
        properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
        // read a empty document with empty bytes
        // fileUpload1: is a .net file upload control
        ContentStream contentStream = new ContentStream
        {
            FileName = formattedName,
            MimeType = "application/msword",
            Length = fileUpload1.FileBytes.Length,
            Stream = new MemoryStream(fileUpload1.FileBytes)
        };
        // this statment would create document in default repository
        folder.CreateDocument(properties, contentStream, null);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ConnectingUsingAtomPub_CreateDocument();
    }
}

